let gameBoard = [];
let currentPlayer = 'black'; // 黑方先行
let gamePaused = false;
let moveHistory = [];

function initializeBoard() {
    const board = document.getElementById('goBoard');
    gameBoard = [];

    for (let row = 0; row < 19; row++) {
        let rowArray = [];
        for (let col = 0; col < 19; col++) {
            const cell = document.createElement('div');
            cell.addEventListener('click', () => placeStone(row, col));
            board.appendChild(cell);
            rowArray.push(null); // 空白位置
        }
        gameBoard.push(rowArray);
    }
}

function placeStone(row, col) {
    if (gamePaused || gameBoard[row][col]) return; // 如果游戏暂停或该位置已被占用，忽略

    // 放置棋子
    gameBoard[row][col] = currentPlayer;
    updateBoard();

    // 保存棋步
    moveHistory.push({ row, col, player: currentPlayer });

    // 切换玩家
    currentPlayer = (currentPlayer === 'black') ? 'white' : 'black';

    // 更新按钮状态
    document.getElementById('undoBtn').disabled = false;
    document.getElementById('aiBtn').disabled = false;
}

function updateBoard() {
    const board = document.getElementById('goBoard');
    let index = 0;

    for (let row = 0; row < 19; row++) {
        for (let col = 0; col < 19; col++) {
            const cell = board.children[index++];
            if (gameBoard[row][col] === 'black') {
                cell.style.backgroundColor = 'black';
                cell.style.borderColor = 'black';
            } else if (gameBoard[row][col] === 'white') {
                cell.style.backgroundColor = 'white';
                cell.style.borderColor = 'black';
            } else {
                cell.style.backgroundColor = '#f4f4f4';
                cell.style.borderColor = '#ddd';
            }
        }
    }
}

function startGame() {
    initializeBoard();
    document.getElementById('startBtn').disabled = true;
    document.getElementById('pauseBtn').disabled = false;
    document.getElementById('undoBtn').disabled = false;
    document.getElementById('aiBtn').disabled = false;
    document.getElementById('restartBtn').disabled = false;
}

function pauseGame() {
    gamePaused = true;
    document.getElementById('pauseBtn').innerText = '恢复游戏';
    document.getElementById('pauseBtn').onclick = resumeGame;
}

function resumeGame() {
    gamePaused = false;
    document.getElementById('pauseBtn').innerText = '暂停游戏';
    document.getElementById('pauseBtn').onclick = pauseGame;
}

function undoMove() {
    if (moveHistory.length === 0) return;

    const lastMove = moveHistory.pop();
    gameBoard[lastMove.row][lastMove.col] = null;
    updateBoard();

    // 切换回上一个玩家
    currentPlayer = (lastMove.player === 'black') ? 'white' : 'black';
}

function aiGuidance() {
    // 模拟的 AI 提示
    alert('AI 提示：下一步可以尝试在 (10, 10) 下子。');
}

function restartGame() {
    // 重置游戏状态和棋盘
    gameBoard = [];
    moveHistory = [];
    currentPlayer = 'black';
    gamePaused = false;
    initializeBoard();

    // 恢复按钮状态
    document.getElementById('startBtn').disabled = true;
    document.getElementById('pauseBtn').disabled = false;
    document.getElementById('undoBtn').disabled = true;
    document.getElementById('aiBtn').disabled = false;
    document.getElementById('restartBtn').disabled = false;
}
